package com.kd.juhaowan.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.kd.juhaowan.R;
import com.kd.juhaowan.http.model.SigninModel;
import com.kd.juhaowan.ui.adapter.BaseRecyclerViewAdapter;
import com.kd.juhaowan.ui.holder.SigninFinishViewHolder;

import java.util.List;


/**
 * Created by Administrator on 2017/6/6 0006.
 */

public class SigninConfirmDialog {

    final Builder builder;
    private final Dialog dialog;

    private SigninConfirmDialog(final Builder builder) {
        this.builder = builder;
        this.dialog = builder.dialog;
    }

    public void show() {
        if (dialog != null&& !dialog.isShowing()) {
            dialog.show();
        }
    }

    public void dismiss() {
        if (dialog != null&& dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public void setOnClickListener(View.OnClickListener listener){
        builder.setOnClickListener(listener);
    }

    public void setCancelable(boolean flag){
        if(dialog != null){
            dialog.setCancelable(flag);
        }
    }

    public static class Builder implements View.OnClickListener {

        private Context context;
        private View contentView;
        private RecyclerView message;
        private Button confirm;
        private Dialog dialog;
        private ImageView closeImage;
        private BaseRecyclerViewAdapter<SigninFinishViewHolder, SigninModel> mNewAdapter;

        public Builder(Context context) {
            this.context = context;
//            final int layoutResId = ResUtils.getLayoutIdentifier(context, "wan_dialog_confirm");
//            final int msgResId = ResUtils.getViewIdentifier(context, "dialog_confirm_message_tv");
//            final int confirmResId = ResUtils.getViewIdentifier(context, "dialog_confirm_btn");
            contentView = LayoutInflater.from(context).inflate(R.layout.dialog_signin_confirm, null);
            message = (RecyclerView) contentView.findViewById(R.id.dialog_content);
            confirm = (Button) contentView.findViewById(R.id.dialog_confirm_btn);
            confirm.setOnClickListener(this);
            closeImage = contentView.findViewById(R.id.dialog_close);
            closeImage.setOnClickListener(this);
            setAdapter();
        }

        private void setAdapter(){
            mNewAdapter = new BaseRecyclerViewAdapter<SigninFinishViewHolder, SigninModel>() {
                @NonNull
                @Override
                public SigninFinishViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                    View view = inflater.inflate(R.layout.item_signin_finish, null, false);
                    SigninFinishViewHolder holder=new SigninFinishViewHolder(view);
                    holder.setOnItemClickListener(getOnItemClickListener());
                    return  holder;

                }

                @Override
                public void onBindViewHolder(@NonNull SigninFinishViewHolder holder, int position) {
                    holder.setDate(getItem(position));

                }
            };
            mNewAdapter.setOnItemClickListener(null);
            mNewAdapter.resetAdapterData(null);
            LinearLayoutManager layoutManager = new LinearLayoutManager(context);
            layoutManager.setOrientation(RecyclerView.HORIZONTAL);

            message.setLayoutManager(layoutManager);
            message.setAdapter(mNewAdapter);
        }

        public Builder setMessage(List<SigninModel> list) {
            mNewAdapter.resetAdapterData(list);
//            if (TextUtils.isEmpty(message)) {
//                this.message.setText("");
//            } else {
//                this.message.setText(message);
//            }
            return this;
        }

        public Builder setConfirmText(String text) {
            if (!TextUtils.isEmpty(text)) {
                this.confirm.setText(text);
            }
            return this;
        }

        public Builder setOnClickListener(View.OnClickListener listener) {
            this.confirm.setOnClickListener(listener);
            return this;
        }

        public SigninConfirmDialog create() {
            dialog = new Dialog(context, R.style.hnssn_dialog);
            dialog.setContentView(contentView);
            dialog.setCanceledOnTouchOutside(false);
            return new SigninConfirmDialog(this);
        }

        @Override
        public void onClick(View v) {

            dialog.dismiss();
        }
    }


}
